﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace websitev2.Core
{
    public class TrophyCore
    {

        public static Models.UsersTrophy[] UsersWithTrophy(int TrophyID, bool complete, int RowsToReturn)
        {

            string _perComplete = "";
            if (complete)
            {
                _perComplete = " t.PercentageComplete>=100";
            }
            else
            {
                _perComplete = " t.PercentageComplete<100";
            }

            //Models.UsersTrophy[] oTrophies = new Models.UsersTrophy(); 

            DataTable dt = SqlAccessCore.ReturnDataTable(@"
SELECT TOP " + RowsToReturn + @" t.UserTrophiesID
      ,t.AwardedOn
      ,t.PercentageComplete
      ,tu.UserName, tu.UserGUID   
  FROM [StartStop].[dbo].[TrophiesUserLink] t
  inner join ssUsers tu on t.UserID = tu.UserID

  where t.TrophyID = " + TrophyID.ToString() + " and " + _perComplete);

            

            if (dt.Rows.Count > 0)
            {
                int _currentRow = 0;
                Models.UsersTrophy[] oTrophies = new Models.UsersTrophy[dt.Rows.Count];
                foreach (DataRow oRow in dt.Rows)
                {
                    Models.UsersTrophy ot = new Models.UsersTrophy();
                    if (oRow["AwardedOn"].ToString() != "")
                    {
                        ot.AwardedOn = (DateTime)oRow["AwardedOn"];
                    }
                    ot.PercentageComplete = (int)oRow["PercentageComplete"];
                    ot.Username = (string)oRow["UserName"];
                    ot.UserAvatarURL = Core.UserCore.ReturnAvatarURL(new Guid(oRow["UserGUID"].ToString()), 2);
                    oTrophies[_currentRow] = ot;
                    _currentRow++;
                }
                return oTrophies;
            }
            else
            {
                return null;
            }
        }


    }
}